package com.blue.bluecode.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.blue.bluecode.sync.ImageLoaderWithDoubleCaches;
import com.blue.bluecode.R;

import java.util.List;

/**
 *
 * Created by bluewaitor on 2015/4/10.
 */
public class AsyncLoaderAdapter extends BaseAdapter implements AbsListView.OnScrollListener {

    private Context mContext;
    private List<String> mData;
//    private ImageDownloader imageDownloader;
//    AsyncImageDownloader downloader;

//    private ImageLoaderWithCaches mImageLoader;
    private ImageLoaderWithDoubleCaches mImageLoader;
    private int mStart = 0, mEnd = 0;
    private boolean mFirstFlag;

    public AsyncLoaderAdapter(Context mContext, List<String> mData) {
        this.mContext = mContext;
        this.mData = mData;
//        imageDownloader = new ImageDownloader();
    }

    public AsyncLoaderAdapter(Context mContext, List<String> mData, ListView listView){
        this.mContext = mContext;
        this.mData = mData;
        mImageLoader = new ImageLoaderWithDoubleCaches(mContext, listView);
        mImageLoader.loadImages(mStart, mEnd);
        mFirstFlag = true;
        listView.setOnScrollListener(this);
    }


    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder mViewHolder = null;
        String url = mData.get(i);
        if(mViewHolder == null){
            mViewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_list_image,null);
            mViewHolder.mImageView = (ImageView) view.findViewById(R.id.ivImage);
            view.setTag(mViewHolder);
        }else{
            mViewHolder = (ViewHolder) view.getTag();
        }
        mViewHolder.mImageView.setTag(url);
        mViewHolder.mImageView.setImageResource(R.mipmap.ic_launcher);
//        imageDownloader.showImageByThread(mViewHolder.mImageView,url);
//        downloader = new AsyncImageDownloader(mViewHolder.mImageView, url);
//        downloader.showImageByAsync(mViewHolder.mImageView,url);
        mImageLoader.showImage(url, mViewHolder.mImageView);
        return view;
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        if(i == SCROLL_STATE_IDLE){
            mImageLoader.loadImages(mStart, mEnd);
        }else{
            mImageLoader.cancelAllTasks();
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {
        mStart = i;
        mEnd = i + i1;
        if(mFirstFlag && i2 > 0){
            mImageLoader.loadImages(mStart, mEnd);
            mFirstFlag = false;
        }
    }

    class ViewHolder {
        public ImageView mImageView;
    }
}
